﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.BidSystem.Model;

public partial class Company_DecPrice : System.Web.UI.Page, IAccess
{

    string action = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        CurrentAccount.CheckSessionTimeOut(this);
        action = Request.QueryString["action"].ToLower();

        if (CurrentAccount.IsDecPrice) //测试用 临时取消
        {
            HZ.Web.MessageBox.Show("解密已成功");
        }
        //判断是否设置报价密码
        if (string.IsNullOrEmpty(CurrentAccount.BidPwd))
        {
            Response.Redirect("CheckBidPwd.aspx?returnurl=DecPrice.aspx");
        }
        if (!CurrentAccount.IsConfirmBid)
        {
            HZ.Web.WebUtil.Alert("没有确认的企业不能解密！！");
            //Response.Redirect(string.Format("ConfirmBid.aspx?action={0}", action));
        }
        if (!CurrentProject.IsDecSwitch)
        {
            HZ.Web.MessageBox.Show("解密开关未开");
        }

        if (CurrentProject.InDecTime < 0)
        {
            HZ.Web.MessageBox.Show("解密时间未到");
        }

        if (CurrentProject.InDecTime > 0)
        {
            HZ.Web.MessageBox.Show("解密时间已过");

        }
        HZ.BidSystem.BLL.BLL_Cache.SetOnlineModel(CurrentAccount.Account.AccountId.Value, new OnLineModel() { Active = "正在考虑解密" });
        SetNav();

        //不用点确认 20秒后自动解密报价


    }
    /// <summary>
    /// 设置导航栏
    /// </summary>
    private void SetNav()
    {
        switch (action)
        {
            case "bargain":
                CompanyNavigation1.BidType = HZ.BidSystem.Model.BidType.Bargain;
                break;

            case "compete":
                CompanyNavigation1.BidType = HZ.BidSystem.Model.BidType.Compete;
                break;
            default:
                CompanyNavigation1.BidType = HZ.BidSystem.Model.BidType.Bid;
                break;
        }
    }

    protected void btndec_Click(object sender, EventArgs e)
    {

        BidType bidtype;
        switch (action)
        {
            case "bargain":
                bidtype = BidType.Bargain;
                break;
            case "compete":
                bidtype = BidType.Compete;
                break;
            default:
                bidtype = BidType.Bid;
                break;
        }

        if (!CurrentAccount.IsConfirmBid)
        {
            HZ.Web.WebUtil.Alert("没有确认报价的产品，不能解密！！");
        }
        else
        {
            if (!CurrentProject.IsDecSwitch)
            {
                HZ.Web.MessageBox.Show("解密开关未开");
            }

            if (CurrentProject.InDecTime < 0)
            {
                HZ.Web.MessageBox.Show("解密时间未到");
            }

            if (CurrentProject.InDecTime > 0)
            {
                HZ.Web.MessageBox.Show("解密时间已过");

            }

            HZ.Common.log4netManager.LogInfo("企业解密", string.Format("用户验证完毕报价密码，准备解密！用户名：{0}，操作IP：{1}，报价密码：。", CurrentAccount.Account.AccountName, HZ.Fun.GetClientIP(), CurrentAccount.BidPwd), null);

            try
            {
                if (BLL_Factory.IBLL_Company.DecPrice(CurrentAccount.Account.AccountId, CurrentAccount.ProjectId, CurrentAccount.BidPwd, CurrentAccount.RandPwd, bidtype) == 1)
                {
                    HZ.Common.log4netManager.LogInfo("企业解密", string.Format("解密成功！用户名：{0}，操作IP：{1}。", CurrentAccount.Account.AccountName, HZ.Fun.GetClientIP()), null);
                    HZ.Web.WebUtil.Alert("解密已成功，感谢您的参与。当您看到解密成功提示时，说明解密成功。 ");
                    //HZ.Web.WebUtil.RedirectByScript(string.Format("PrePrintPrice.aspx?action=bid"));
                    Response.Write("<script>window.location.href='PrePrintPrice.aspx?action=bid'</script>");
                    
                }
                else
                {
                    HZ.Common.log4netManager.LogInfo("企业解密", string.Format("解密失败！用户名：{0}，操作IP：{1}。", CurrentAccount.Account.AccountName, HZ.Fun.GetClientIP()), null);
                    HZ.Web.WebUtil.Alert("解密失败，请联系管理员！！", this);
                }
            }
            catch (Exception ex)
            {
                HZ.Common.log4netManager.LogInfo("企业解密", string.Format("解密异常！用户名：{0}，操作IP：{1}，异常数据：{2}。", CurrentAccount.Account.AccountName, HZ.Fun.GetClientIP(), ex.ToString()), null);
                HZ.Web.WebUtil.Alert("解密出现异常，请联系管理员！！", this);
            }
        }
    }

    #region IAccess Members

    IList<EnumAccess> IAccess.AccessList
    {
        get
        {
            var list = new List<EnumAccess>();
            list.Add(EnumAccess.Company);
            return list;
        }

    }

    #endregion
}
